package Servlet;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.cyy.bean.user;
import com.cyy.dao.*;

@WebServlet("/login")
public class loginServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("开始登录...");
//        清空session
        request.getSession().invalidate();
        request.setCharacterEncoding("UTF-8");
//        获取表单提交的登录者身份,姓名,密码
        String identity = request.getParameter("identity");
        String name = request.getParameter("name");
        String pwd = request.getParameter("pwd");
        String message="";
//         设置警告信息
        if (identity == null || identity.equals("null")) {
            message = "请选择您的身份！";
            System.out.println(message);
            request.setAttribute("message", message);
            request.getRequestDispatcher("/login.jsp").forward(request, response);
        }
        else {
            boolean exists = false;
//            顾客模块
            if (identity.equals("customer")) {
//                判断该登录者是否存在于表user中(已注册)
                userDao userDao = new userDao();
                try {
                    exists = userDao.selectNamePwd(name, pwd);
                } catch (SQLException | ClassNotFoundException throwables) {
                    throwables.printStackTrace();
                }
                if (exists) {
                    user user1=new user();
                    try {
                        user1 = userDao.selectAll(name);
                    } catch (SQLException | ClassNotFoundException throwables) {
                        throwables.printStackTrace();
                    }
                    message = "登录成功！欢迎顾客" + name + "!";
                    System.out.println(message);
//                    添加登录记录到loginRecord中
                    Date date = new Date();
//                    设置日期格式
                    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    String dateTime = df.format(date);
                    loginRecordDao loginRecordDao = new loginRecordDao();
                    try {
                        loginRecordDao.addRecord(name,dateTime,0);
                    } catch (SQLException | ClassNotFoundException throwables) {
                        throwables.printStackTrace();
                    }
                    request.getSession().setAttribute("identity", 0);
                    request.getSession().setAttribute("name", name);
                    request.getSession().setAttribute("address", user1.getAddress());
                    request.getSession().setAttribute("phone", user1.getPhone());
                    request.getSession().setAttribute("email", user1.getEmail());
                    request.getRequestDispatcher("/showIndex").forward(request, response);
                    return;
                } else {
                    message = "顾客账号或密码错误！请重新输入！";
                }
//                售货员模块
            } else if (identity.equals("salesman")) {
                System.out.println("登录者身份:售货员!");
                salesmanDao salesmanDao = new salesmanDao();
                try {
                    exists = salesmanDao.selectNamePwd(name, pwd);
                } catch (SQLException | ClassNotFoundException throwables) {
                    throwables.printStackTrace();
                }
                if (exists) {
                    String email = null;
                    try {
                        email = salesmanDao.selectEmail(name);
                    } catch (SQLException | ClassNotFoundException throwables) {
                        throwables.printStackTrace();
                    }
                    message = "登录成功！欢迎售货员" + name + "!";
//                    添加登录记录到loginRecord中
                    Date date = new Date();
                    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
                    String dateTime = df.format(date);
                    loginRecordDao loginRecordDao = new loginRecordDao();
                    try {
                        loginRecordDao.addRecord(name,dateTime,1);
                    } catch (SQLException | ClassNotFoundException throwables) {
                        throwables.printStackTrace();
                    }
                    System.out.println(message);
                    request.getSession().setAttribute("name", name);
                    request.getSession().setAttribute("identity", 1);
                    request.getSession().setAttribute("email", email);
                    request.getRequestDispatcher("/showProduct").forward(request, response);
                    return;
                } else {
                    message = "销售员账号或密码错误！请重新输入！";
                }
            }
            System.out.println(message);
            request.setAttribute("message", message);
            request.getRequestDispatcher("/login.jsp").forward(request, response);
            return;
        }
    }
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request,response);
    }
}
